﻿using System;
using System.Collections.Generic;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Base.Dao;
using T.FAS.Runtime.Base.Entity;

namespace T.FAS.Runtime.DataModel.Dao
{
    /// <summary>
    /// CNO ELB数据访问类
    /// </summary>
    public class AssociateItemELBDao : DaoService<AssociateItem>
    {
        public override string TableName => "fas_associationitem";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "AssociationID";
        public string MetaDataForeignColumn => "CompositeDataObjectID";

        public AssociateItemELBDao() : base()
        {
            DBName = "TFAS";
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<AssociateItem> GetAssociateItems(string assoId)
        {
            var result = new List<AssociateItem>();
            if (string.IsNullOrEmpty(assoId))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{ForeignColumn}='{assoId}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as AssociateItem));
            }
            return result;
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<AssociateItem> GetAssociateItemsByMetaDataForeignKey(string metaDataID)
        {
            var result = new List<AssociateItem>();
            if (string.IsNullOrEmpty(metaDataID))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{MetaDataForeignColumn}='{metaDataID}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as AssociateItem));
            }
            return result;
        }

        /// <summary>
        /// 添加AssociateItem
        /// </summary>
        /// <param name="assoItems"></param>
        public void CreateAssociateItem(List<AssociateItem> assoItems)
        {
            try
            {
                base.Add(assoItems);
            }
            catch (Exception e)
            {

                throw new FasException(DataModelErrorCode.CDO.CreateAssoItemError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除AssociateItem
        /// </summary>
        /// <param name="AssociateItems"></param>
        public void DeleteAssociateItem(string assoId)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, assoId));

            }
            catch (Exception e)
            {

                throw new FasException(DataModelErrorCode.CDO.DeleteAssoItemError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除AssociateItem
        /// </summary>
        /// <param name="metaDataID"></param>
        public void DeleteByMetaDataForeignKey(string metaDataID)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.MetaDataForeignColumn, metaDataID));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.CDO.DeleteAssoItemError, e.Message, e);
            }
        }
    }
}
